package com.ftg.shopping.sp_goodsinfo.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController("abc")
@Api
@RequestMapping("/api/upload/uploadtemp")
public class uploadController {

    @RequestBody
    @PostMapping("/fileUpload")
    public String fileUpload(@RequestParam(value = "filed") MultipartFile file, Model model, HttpServletRequest request) {

        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "C:\\Users\\Administrator\\Desktop\\nginx-1.19.4\\secound\\shangcheng\\res\\static\\img\\"; // 上传后的路径
        fileName = "juzi" + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "/upload/" + fileName;
        model.addAttribute("filename", filename);
        return "filed";
    }

}
